package com.rwdesign.springboot.Waimai.controller;

import com.rwdesign.springboot.Waimai.entity.Goods;
import com.rwdesign.springboot.Waimai.entity.Shop;
import com.rwdesign.springboot.Waimai.service.GoodsService;
import com.rwdesign.springboot.Waimai.service.ShopService;
import com.rwdesign.springboot.Waimai.service.UserService;
//import com.rwdesign.springboot.Waimai.utils.JsonUtils;
import com.rwdesign.springboot.Waimai.utils.ResultBody;
import com.rwdesign.springboot.Waimai.vo.AddGoodsVo;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author cdp
 * @create 2022-01-29 13:06
 */


@CrossOrigin
@RestController
@RequestMapping("/waimai/sys")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserService userService;

    public boolean isLogin(String userToken) {
        if (userToken == null) {
            return false;
        } else if (userService.selectIdByToken(userToken) != null) {
            return true;
        } else {
            return false;
        }
    }

    // 查询是否为商户
    public boolean isShoper(String userToken) {
        if (userToken == null) {
            return false;
        } else if ("1".equals(userService.selectIsShoperByUserId(userService.selectIdByToken(userToken)))) {
            return true;
        } else {
            return false;
        }
    }

    // 查询商品是否属于该店铺
    public boolean isShoperGoods(Integer shopId, Integer goodsId) {
        if (goodsId == null) {
            return false;
        } else if (shopId.equals(goodsService.selectShopIdByGoodsId(goodsId))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商品分类列表
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getAllGoodsTypes", method = RequestMethod.GET)
    public ResultBody getAllGoodsTypes(HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        }
        System.out.println(request.getHeader("Authorization"));

        List<String> list = goodsService.selectAllGoodsTypes(Integer.parseInt(request.getParameter("shopId")));

        if (list != null) {
            Map data = new HashMap();
            data.put("typeList", list);
            return ResultBody.isOk(200, "success", data);
        } else {
            return ResultBody.isError(500, "error");
        }

    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商品詳情
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getAllGoods", method = RequestMethod.GET)
    public ResultBody getAllGoods(HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        }

        System.out.println(request.getHeader("Authorization"));

        List<Goods> list = goodsService.selectAllGoods(request.getParameter("goodsType"), Integer.parseInt(request.getParameter("shopId")));

        if (list != null) {
            Map data = new HashMap();
            data.put("data", list);
            return ResultBody.isOk(200, "success", data);
        } else {
            return ResultBody.isError(500, "error");
        }

    }


    //  商户权限---------------------------------------------------------------------------------------------------------------------------------------------------------

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 新增商品
     * @Date 21:25 2022/3/7
     * @Param
     **/
    @RequestMapping(value = "/addGoods", method = RequestMethod.POST)
    public ResultBody addGoods(@RequestBody(required = false) AddGoodsVo addGoodsVo, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {
            System.out.println(addGoodsVo.getGoodsLists());
            if (addGoodsVo.getGoodsLists().size() == 0) {
                return ResultBody.isError(205, "参数为空");
            } else {
                for (int i = 0; i < addGoodsVo.getGoodsLists().size(); i++) {
                    Goods goods = new Goods();
                    goods.setGoodsId(null);
                    goods.setGoodsName(addGoodsVo.getGoodsLists().get(i).getGoodsName());
                    goods.setGoodsInfo(addGoodsVo.getGoodsLists().get(i).getGoodsInfo());
                    goods.setGoodsType(addGoodsVo.getGoodsLists().get(i).getGoodsType());
                    goods.setGoodsPicture(addGoodsVo.getGoodsLists().get(i).getGoodsPicture());
                    goods.setGoodsPrice(addGoodsVo.getGoodsLists().get(i).getGoodsPrice());
                    goods.setShopId(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));
                    if (goodsService.addGoods(goods) != 1) {
                        return ResultBody.isError(500, "error");
                    }
                }
                return ResultBody.isOk(200, "success");
            }

        }

    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 删除商品
     * @Date 21:25 2022/3/7
     * @Param
     **/
    @RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
    public ResultBody deleteGoods(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {
            String[] goodsIdArray = null;
            goodsIdArray = requestInfo.get("goodsId").split(",");
            for (int j = 0; j < goodsIdArray.length; j++) {
                if (!isShoperGoods(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))), Integer.parseInt(goodsIdArray[j]))) {
                    return ResultBody.isError(256, "商品不属于该店铺");
                }
            }
            for (int j = 0; j < goodsIdArray.length; j++) {
                if (goodsService.deleteGoodsById(Integer.parseInt(goodsIdArray[j])) != 1) {
                    return ResultBody.isError(205, "操作失败");
                }
            }

            return ResultBody.isOk(200, "success");
        }

    }


    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商户商铺分类
     * @Date 21:25 2022/3/7
     * @Param
     **/
    @RequestMapping(value = "/getShoperGoodsTypes", method = RequestMethod.GET)
    public ResultBody getShoperGoodsTypes(@RequestBody(required = false) Map<String, String> requestInfo, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {
            List<String> list = goodsService.selectAllGoodsTypes(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));

            if (list != null) {
                Map data = new HashMap();
                data.put("typeList", list);
                return ResultBody.isOk(200, "success", data);
            } else {
                return ResultBody.isError(500, "error");
            }
        }

    }


    /**
     * @return
     * @Author chendanping
     * @Description //TODO 查询商户商品詳情
     * @Date 20:59 2022/3/1
     * @Param
     **/
    @RequestMapping(value = "/getShoperGoods", method = RequestMethod.GET)
    public ResultBody getShoperGoods(HttpServletRequest request) {
        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {

            System.out.println(request.getHeader("Authorization"));

            List<Goods> list = goodsService.selectAllGoods(request.getParameter("goodsType"), userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));

            if (list != null) {
                Map data = new HashMap();
                data.put("data", list);
                return ResultBody.isOk(200, "success", data);
            } else {
                return ResultBody.isError(500, "error");
            }
        }
    }

    /**
     * @return
     * @Author chendanping
     * @Description //TODO 编辑商品
     * @Date 21:25 2022/3/7
     * @Param
     **/
    @RequestMapping(value = "/updataGoods", method = RequestMethod.POST)
    public ResultBody updataGoods(@RequestBody(required = false) AddGoodsVo addGoodsVo, HttpServletRequest request) {
        System.out.println(request.getHeader("Authorization"));

        if (!isLogin(request.getHeader("Authorization"))) {
            return ResultBody.isError(402, "Token错误或已过期");
        } else if (!isShoper(request.getHeader("Authorization"))) {
            return ResultBody.isError(401, "非商家，身份验证失败");
        } else if (userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))) == null) {
            return ResultBody.isError(255, "暂未开店");
        } else {
            System.out.println(addGoodsVo.getGoodsLists());

            if (addGoodsVo.getGoodsLists().size() == 0) {
                return ResultBody.isError(205, "参数为空");
            } else {
                for (int i = 0; i < addGoodsVo.getGoodsLists().size(); i++) {
                    if (!isShoperGoods(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))), addGoodsVo.getGoodsLists().get(i).getGoodsId())) {
                        return ResultBody.isError(256, "商品不属于该店铺");
                    }
                }

                for (int i = 0; i < addGoodsVo.getGoodsLists().size(); i++) {
                    Goods goods = new Goods();
                    goods.setGoodsId(addGoodsVo.getGoodsLists().get(i).getGoodsId());
                    goods.setGoodsName(addGoodsVo.getGoodsLists().get(i).getGoodsName());
                    goods.setGoodsInfo(addGoodsVo.getGoodsLists().get(i).getGoodsInfo());
                    goods.setGoodsType(addGoodsVo.getGoodsLists().get(i).getGoodsType());
                    goods.setGoodsPicture(addGoodsVo.getGoodsLists().get(i).getGoodsPicture());
                    goods.setGoodsPrice(addGoodsVo.getGoodsLists().get(i).getGoodsPrice());
                    goods.setShopId(userService.selectShopIdByUserId(userService.selectIdByToken(request.getHeader("Authorization"))));
                    if (goodsService.updataGoods(goods) != 1) {
                        return ResultBody.isError(500, "error");
                    }
                }


            }
            return ResultBody.isOk(200, "success");
        }

    }

}
